﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PageView.SystemManager;

namespace CommunitySystem
{
    public partial class RoleManagerControl : UserControl
    {

        SystemService.SystemServiceClient Client = new SystemService.SystemServiceClient();
        SystemService.Role RoleModel = new SystemService.Role();
        public RoleManagerControl()
        {
            InitializeComponent();

            LoadData();
        }
        /// <summary>
        /// 加载角色数据
        /// </summary>
        void LoadData()
        {
            Client.GetRoleCompleted += new EventHandler<SystemService.GetRoleCompletedEventArgs>(Client_GetRoleCompleted);
            Client.GetRoleAsync();
        }

        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Client_GetRoleCompleted(object sender, SystemService.GetRoleCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            RoleMangerWindow RMWindow = new RoleMangerWindow();
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RMWindow_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            LoadData();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as SystemService.Role;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                RoleMangerWindow RMWindow = new RoleMangerWindow(seljw);
                ///注册关闭事件
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as SystemService.Role;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                Client.DeleteRoleCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_DeleteRoleCompleted);
               
                Client.DeleteRoleCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_DeleteRoleCompleted);
                Client.DeleteRoleAsync(seljw.ID);
            }
        }

        void Client_DeleteRoleCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadData();
        }

        /// <summary>
        /// 编辑权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as SystemService.Role;
            if (seljw != null)
            {
                RoleMenuWindow rmw = new RoleMenuWindow(seljw.ID);
                rmw.Show();
            }
        }
    }
}
